package org.idtem.component.wordnet;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

import org.idtem.component.wordnet.connection.WordNetConnection;
import org.idtem.component.wordnet.datatypes.DataTypeRelation;
import org.idtem.component.wordnet.datatypes.DataTypeSynset;
import org.idtem.component.wordnet.datatypes.DataTypeVariant;


public class DBSpanishWordNetAPI 
{

	private static DBSpanishWordNetAPI _DBSpanishWordNetAPI = null;
	
	private DBSpanishWordNetAPI()
	{
	}
	
	public static DBSpanishWordNetAPI getInstance()
	{
		if (_DBSpanishWordNetAPI == null)
		{
			_DBSpanishWordNetAPI = new DBSpanishWordNetAPI();
		}
		return _DBSpanishWordNetAPI;
	}
	
	
	public Collection<DataTypeVariant> getVariants(DataTypeSynset synset)
	{
		 
		WordNetConnection wnConn = WordNetConnection.getInstance();
		
        //Connection conn = null;
        //java.sql.PreparedStatement prepareStm = null;
        ArrayList<DataTypeVariant> variants = new ArrayList<DataTypeVariant>();
        try 
        {
            //conn = wnConn.getConnection();
        	
        	wnConn.getVariantsPE().setString(1, Character.toString(synset.getPos()));
        	wnConn.getVariantsPE().setString(2, synset.getOffset());
            
            ResultSet result = wnConn.getVariantsPE().executeQuery();
            
            while (result.next()){
            	String word = new String(result.getBytes(3), "UTF8");
            	int sense = result.getInt(4);
            	float csco = result.getFloat(5);
            	String statusObj =  result.getString(6);
            	
            	DataTypeVariant variantDataType = new DataTypeVariant(synset, word, sense, csco, statusObj.charAt(0));
            	variants.add(variantDataType);
            }
            
            
            
            //prepareStm.close();
            //prepareStm = null;

            //conn.close();
            //conn = null;
        } catch (Exception e) {
			e.printStackTrace();
		} finally {
            /*
             * close any jdbc instances here that weren't
             * explicitly closed during normal code path, so
             * that we don't 'leak' resources...
             */

            //if (prepareStm != null) {
            //    try {
            //   	prepareStm.close();
            //    } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here
           //     }

            //    prepareStm = null;
            //}

            //if (conn != null) {
            //    try {
            //        conn.close();
            //    } catch (SQLException sqlex) {
            //        // ignore -- as we can't do anything about it here
            //    }

            //    conn = null;
            //}
        }
        return variants;
	}

	public DataTypeSynset getSynset(char pos, String offset) {
		WordNetConnection wnConn = WordNetConnection.getInstance();
		
		
        //Connection conn = null;
        //java.sql.PreparedStatement prepareStm = null;
        DataTypeSynset synset = null;
        try 
        {
        	
            //prepareStm = conn.prepareStatement("SELECT pos, offset, sons, lexical, gloss FROM eswn_synset where pos = ? and offset=?");
        	wnConn.getSynsetPE().setString(1, Character.toString(pos));
        	wnConn.getSynsetPE().setString(2, offset);
            
            ResultSet result = wnConn.getSynsetPE().executeQuery();
            
            if (result.next()){
            	String p=result.getString(1);		//pos	
            	String o=result.getString(2);		//offset
            	String sons=result.getString(3);	//sons
            	String lexical=result.getString(4);	//lexical
            	String gloss = "";
            	if (result.getBytes(5) == null || result.getBytes(5).length == 0)
            		gloss = "";
            	else
            		gloss = new String(result.getBytes(5));	//gloss
            		
            	synset = new DataTypeSynset(p.charAt(0),o,sons,lexical,gloss, 0);
            }
            
            //prepareStm.close();
           // prepareStm = null;

            //conn.close();
            //conn = null;
        } catch (Exception e) {
			e.printStackTrace();
		} finally {
            /*
             * close any jdbc instances here that weren't
             * explicitly closed during normal code path, so
             * that we don't 'leak' resources...
             */

            //if (prepareStm != null) {
            //    try {
            //    	prepareStm.close();
            //    } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here
            //    }

            //    prepareStm = null;
            //}

            //if (conn != null) {
            //    try {
            //        conn.close();
            //    } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here
            //    }

            //   conn = null;
            //}
        }
        return synset;
	}
	
	public Collection<DataTypeRelation> getRelations(DataTypeSynset dataTypeSynset, String relation, int deep, boolean direct) throws Exception {
		WordNetConnection wnConn = WordNetConnection.getInstance();
		
        //Connection conn = null;
        //java.sql.PreparedStatement prepareStm = null;
        ArrayList<DataTypeRelation> relations = new ArrayList<DataTypeRelation>();
        try 
        {
        	String sourceOffset = dataTypeSynset.getOffset();
        	char sourcePOS = dataTypeSynset.getPos(); 
            //conn = wnConn.getConnection();
        	java.sql.PreparedStatement prepareStm = null;
    		if (direct){
    			prepareStm = wnConn.getRelationDirectPE();
    		}else{
    			prepareStm = wnConn.getRelationInversePE();
    		}
    		prepareStm.setString(1, relation);
    		prepareStm.setString(2, Character.toString(sourcePOS));
    		prepareStm.setString(3, sourceOffset);    		
            ResultSet result = prepareStm.executeQuery();

            while (result.next()){
            	String p=result.getString(1);		//pos	
            	String o=result.getString(2);		//offset
            	String sons=result.getString(3);	//sons
            	String lexical=result.getString(4);	//lexical
            	String gloss = "";
            	if (result.getBytes(5) == null || result.getBytes(5).length == 0)
            		gloss = "";
            	else
            		gloss = new String(result.getBytes(5));	//gloss
            	int csco = result.getInt(6); // csco relation
            		
            	DataTypeSynset targetSynset = new DataTypeSynset(p.charAt(0),o,sons,lexical,gloss, deep + 1);
            	DataTypeRelation relationDataType = new DataTypeRelation(dataTypeSynset, targetSynset, relation, csco, deep + 1); 
            	relations.add(relationDataType);
            }
            
            
            //prepareStm.close();
            //prepareStm = null;

            //conn.close();
            // conn = null;
        } catch (Exception e) {
			e.printStackTrace();
		} finally {
            /*
             * close any jdbc instances here that weren't
             * explicitly closed during normal code path, so
             * that we don't 'leak' resources...
             */

            //if (prepareStm != null) {
            //    try {
            //    	prepareStm.close();
            //    } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here
            //    }

            //    prepareStm = null;
            //}

            //if (conn != null) {
            //    try {
            //        conn.close();
            //    } catch (SQLException sqlex) {
                    // ignore -- as we can't do anything about it here
            //    }

            //     conn = null;
            //}
        }
        return relations;
	}
	
	
	
	
	public Collection<DataTypeVariant> getVariants(char pos, String lema) {
		WordNetConnection wnConn = WordNetConnection.getInstance();
		
		
		ArrayList<DataTypeVariant> variants = new ArrayList<DataTypeVariant>();
        try 
        {
            //conn = wnConn.getConnection();
        	
        	wnConn.getVariantsTogetOffsetsPE().setString(1, Character.toString(pos));
        	wnConn.getVariantsTogetOffsetsPE().setString(2, lema);
            
            ResultSet result = wnConn.getVariantsTogetOffsetsPE().executeQuery();
            
            while (result.next()){
            	String offset = result.getString(2);
            	String word = new String(result.getBytes(3), "UTF8");
            	int sense = result.getInt(4);
            	float csco = result.getFloat(5);
            	String statusObj =  result.getString(6);
            	char status = ' ';
            	if (statusObj != null && statusObj.length() > 0)
            		status = statusObj.charAt(0);
            	DataTypeVariant variantDataType = new DataTypeVariant(pos, offset, word, sense, csco, status);
            	variants.add(variantDataType);
            	
            }
        } catch (Exception e) {
			e.printStackTrace();
		} finally {


        }
        return variants;
	}


	
	
}
	
	